/*Diego Lima de Freitas RA: 317632
 *Yuri Souza Padua RA: 317420
 * 
 * Classe que trata expressões
 */

package AST;

import Lexer.Symbol;

public class CompositeExpr extends Expr {

    private Expr left, right;
    private int oper;
    private static String[] arrayOper;

    static {
        arrayOper = new String[Symbol.LastSymbol];
        int i;
        for (i = 0; i < arrayOper.length; i++) {
            arrayOper[i] = "";
        }
        arrayOper[Symbol.PLUS] = "+";
        arrayOper[Symbol.MINUS] = "-";
        arrayOper[Symbol.MULT] = "*";
        arrayOper[Symbol.DIV] = "/";
        arrayOper[Symbol.REMAINDER] = "%";
        arrayOper[Symbol.LT] = "<";
        arrayOper[Symbol.LE] = "<=";
        arrayOper[Symbol.GT] = ">";
        arrayOper[Symbol.GE] = ">=";
        arrayOper[Symbol.NEQ] = "#";
        arrayOper[Symbol.EQ] = "==";
        arrayOper[Symbol.ASSIGN] = "=";
        arrayOper[Symbol.AND] = "&&";
        arrayOper[Symbol.OR] = "||";
    }

    public CompositeExpr(Expr pleft, int poper, Expr pright) {
        left = pleft;
        oper = poper;
        right = pright;
    }

    @Override
    public void genC(PW pw, boolean putParenthesis) {
        if (putParenthesis) {
            pw.out.print("(");
        }
        left.genC(pw, true);
        pw.out.print(" " + arrayOper[oper] + " ");
        right.genC(pw, true);
        if (putParenthesis) {
            pw.out.print(")");
        }
    }

    @Override
    public Type getType() {
        
        if (oper == Symbol.EQ || oper == Symbol.NEQ || oper == Symbol.LE || oper == Symbol.LT
                || oper == Symbol.GE || oper == Symbol.GT) {

            return Type.booleanType;
        } else if (oper == Symbol.AND || oper == Symbol.OR) {
            return Type.booleanType;
        } else {
            return Type.integerType;
        }
    }
}
